package com.example.myapplication.base.network.rxJava;




import com.example.myapplication.base.mvvm.BaseViewModel;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 自定义的Observer
 */

public abstract class MyObserver<T> implements Observer<T> {

    private final BaseViewModel presenter;
    private Disposable disposable;

    public MyObserver(BaseViewModel presenter) {
        this.presenter = presenter;
    }


    @Override
    public void onError(Throwable throwable) {
        if (presenter != null) {
            presenter.loadError(throwable);
        }
    }

    @Override
    public void onComplete() {
        if (presenter != null) {
            presenter.loadComplete();
            if(disposable!=null)
            presenter.removeTask(disposable);
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable=d;
        if (presenter != null) {
            presenter.addTask(disposable);
        }
    }
}
